package com.tencent.qf.common.gateway.qwRobot;


import com.tencent.qf.common.zhiyan.ZhiyanLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author: p_haojiawu@tencent.com
 * @date: 2023-06-06 18:27
 **/

@Service
@Slf4j
public class QwRobotService {

    @Resource
    private AreaTransferProperties areaTransferProperties;

    @Resource
    private QwRobotClient qwRobotClient;

    @Resource
    private QwRobotUtil qwRobotUtil;

    /**
     * 发送商品转链上限告警
     * @param ruleName 规则名称
     * @param transferLimit 每日上限
     * @param transferCount 今日已转链
     * @param insufficientPercentage 不足百分比
     */
    public boolean sendTransferAlarm(String ruleName, long transferLimit, long transferCount, Integer insufficientPercentage){
        HashMap<String, String> params = new HashMap<>();
        params.put("ruleName", ruleName);
        params.put("transferLimit", String.valueOf(transferLimit));
        params.put("transferCount", String.valueOf(transferCount));
        params.put("insufficientPercentage", String.valueOf(insufficientPercentage));
        String content = qwRobotUtil.parseTemplate(params, areaTransferProperties.getAlarmTemplate());
        String markdown = qwRobotUtil.createPostData(areaTransferProperties.getChatId(), "markdown", content);
        try {
            qwRobotClient.post(markdown);
            return true;
        }catch (Exception e) {
            log.error("发送商品转链上限告警失败， content: {},error:", content, e);
            ZhiyanLogUtil.sendLog(MDC.get("trace_id"),String.format("发送商品转链上限告警失败， 规则名称: %s", ruleName));
            return false;
        }
    }

}
